package main;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

@SuppressWarnings("serial")
class BottomPanel extends JPanel implements ActionListener {
	ApproximationPanel target;
	JRadioButton rb_add;
	JRadioButton rb_delete;
	JButton clear;
	String st_add_label = "Add Points";
	String st_delete_label = "Delete Points";

	public BottomPanel(ApproximationPanel target) {
		super(new GridLayout(1, 0));
		this.target = target;
		setLayout(new FlowLayout(FlowLayout.CENTER));
		
		rb_add = new JRadioButton(st_add_label);
		rb_add.setActionCommand("add");
		rb_add.setSelected(true);
		rb_add.addActionListener(this);

		rb_delete = new JRadioButton(st_delete_label);
		rb_delete.setActionCommand("delete");
		rb_delete.setSelected(false);
		rb_delete.addActionListener(this);

		clear = new JButton("Clear");
		clear.setActionCommand("clear");
		clear.setSelected(false);
		clear.addActionListener(this);

		add(rb_add);
		add(rb_delete);
		add(clear);
	}

	public void actionPerformed(ActionEvent e) {
		if ("add".equals(e.getActionCommand())) {
			rb_add.setSelected(true);
			rb_delete.setSelected(false);
			target.setAction(ApproximationPanel.ADD);
		} else if ("delete".equals(e.getActionCommand())) {
			rb_add.setSelected(false);
			rb_delete.setSelected(true);
			target.setAction(ApproximationPanel.DELETE);
		} else if ("clear".equals(e.getActionCommand())) {
			target.clear();
			target.setAction(ApproximationPanel.ADD);
			rb_add.setSelected(true);
			rb_delete.setSelected(false);
		}
	}
}
